<?php
class BPDEV_Youtube {

	var $num_items,$type, $tags, $imagesize, $before_image, $after_image, $id_number, $set_id, $full_url, $thumb_url, $title, $all, $author, $user_id;

	function BPDEV_Youtube( $userid = false ) {

		global $bp;

		if ( $userid ) {

			$this->user_id = $userid;

		} else {

			$this->user_id = $bp->displayed_user->id;

		}

		$this->type = get_usermeta( $this->user_id,   'bpdev_youtube_set_show2type' );
		$this->tags = trim( get_usermeta( $this->user_id , 'bpdev_youtube_set_tags' ) );
		$this->id_number = stripslashes( get_usermeta( $this->user_id , 'bpdev_youtube_set_youtubeid' ) );
		$this->set_id = stripslashes( get_usermeta( $this->user_id , 'bpdev_youtube_set_set' ) );
		$this->parse_youtube();

	}
	function parse_youtube() {

		error_reporting(E_ERROR);

		if ($this->type == "upload") { $rss_url = 'http://gdata.youtube.com/feeds/base/users/' . $this->id_number . '/uploads?alt=atom'; }
		elseif ($this->type == "favourite") { $rss_url = 'http://gdata.youtube.com/feeds/base/users/' . $this->id_number . '/favorites?alt=atom'; }

// 		elseif ($this->type == "favorite") { $rss_url = 'http://api.youtube.com/services/feeds/photos_faves.gne?id=' . $this->id_number . '&format=atom'; }
// 		elseif ($this->type == "set") { $rss_url = 'http://api.youtube.com/services/feeds/photoset.gne?set=' . $this->set_id . '&nsid=' . $this->id_number . '&format=atom'; }
// 		elseif ($this->type == "group") { $rss_url = 'http://api.youtube.com/services/feeds/groups_pool.gne?id=' . $this->id_number . '&format=atom'; }
// 		elseif ($this->type == "community" || $this->type == "public") { $rss_url = 'http://api.youtube.com/services/feeds/photos_public.gne?tags=' . $this->tags . '&format=atom'; }

		else { $this->error = "This user hasn't set up this page."; }

		if ( function_exists( 'simplepie_init' ) and get_option( 'bpdev-simplepie-status' ) != "off" ) {

			simplepie_init();

			$rss = new SimplePie($rss_url);
			$rss->handle_content_type();

			if ($rss) {

				$this->imgurl = "";
				# specifies number of pictures
				$items = array_slice($rss->get_items(), 0, $this->num_items);
				$count = 0;
				foreach ( $items as $item ) {

					$author = $item->get_author();
					$this->author = $author->get_name();
					$this->url = $item->get_permalink();
					$this->title = $item->get_title();
					$this->content = $item->get_description();
					$parts = explode("=", $this->url);  
					$this->thumb = "http://img.youtube.com/vi/" . $parts[1] . "/default.jpg";
					$count = $count + 1;

					$this->all[$count] = "<li>\n\t<div class=\"wire-post-metadata\">\n\t\t<img  width=\"80\" class=\"avatar photo\" alt=\"\" src=\"". $this->thumb . "\"/>Published by ". $this->author ." on XXXDATE</div>\n\t<div class=\"yt-post-content\"><p><a href=\"". $this->url ."\" rel=\"vidbox\" title=\"". $this->title ."\">". $this->title ."</a></p></div>\n</li>";
				
					$this->all2[$count] = "<li>\n\t<a href=\"". $this->url ."\" title=\"". $this->title ."\" rel=\"vidbox\" ><img  width=\"80\" class=\"avatar photo\" alt=\"". $this->title ."\" src=\"". $this->thumb . "\" title=\"". $this->title ."\" /></a>\n</li>";

				}

			}

			if ( $this->error ) { $this->author = "Not Definied Yet"; }

		}

	}

	function image_link() {

		if ($this->error) { 

			echo $this->error; 

		} else {

			$items = array_slice($this->all, 0, 5);
			foreach ( $items as $one ) {

				echo $one;

			}

		}

	}

	function image_horiz_link() {
		if ($this->error) { 

			echo $this->error; 

		} else {

			if ( $this->all ) {

				$items = array_slice($this->all2, 5, 10);
				foreach ( $items as $one ) {

					echo $one;

				}

			} else {

				echo $this->error;

			}

		}

	}

	function profile_link() {
	
		if ($this->error) { 

			echo $this->error; 

		} else {

			if ( $this->all ) {

				$items = array_slice($this->all, 0, 2);
				foreach ( $items as $one ) {

					echo $one;

				}

			} else {

				echo $this->error;

			}

		}

	}

}
?>